﻿
function TopMenuHover(item) {
    // change image
    item.className = item.hiliteclass;
    var img = item.firstChild.getElementsByTagName('img');
    img[0].src = item.ighovimage;
}

function TopMenuOut(item) {
    item.className = item.igclass;
    var img = item.firstChild.getElementsByTagName('img');
    img[0].src = item.igimage;
}

function TopMenuClick(menuid, url) {
    if (url == "/" || url == "") {
        alert(ToMeZone.Scripts.Resources.Common.sC_NoPermissionToAccessMenu);
        return;
    }
    SC_Menu.navigateMenu(menuid, url);
}

function ItemExists(listBox, itemValue) {
    for (var i = 0; i < listBox.options.length; i++) {
        if (listBox.options[i].value == itemValue) {
            return true;
        }
    }

    return false;
}

function ChangeListItemOrder(listBox, isUp, btn) {
    var selIdx = listBox.selectedIndex;
    if ((isUp && selIdx == 0) || (!isUp && selIdx == listBox.options.length - 1) || listBox.options.length == 0) {
        return;
    }

    var newList = new Array();
    var idx = 0;
    var obj;
    var nextIdx = isUp ? selIdx - 2 : selIdx - 1;

    while (idx <= nextIdx) {

        obj = new Object();
        obj.itemValue = listBox.options[idx].value;
        obj.itemName = listBox.options[idx].text;

        newList[idx] = obj;
        idx = idx + 1;
    }

    if (isUp) {
        obj = new Object();
        obj.itemValue = listBox.options[selIdx].value;
        obj.itemName = listBox.options[selIdx].text;
        newList[idx] = obj;
        idx = idx + 1;

        obj = new Object();
        obj.itemValue = listBox.options[selIdx - 1].value;
        obj.itemName = listBox.options[selIdx - 1].text;
        newList[idx] = obj;
        idx = idx + 1;
    }
    else {
        obj = new Object();
        obj.itemValue = listBox.options[selIdx + 1].value;
        obj.itemName = listBox.options[selIdx + 1].text;
        newList[idx] = obj;
        idx = idx + 1;

        obj = new Object();
        obj.itemValue = listBox.options[selIdx].value;
        obj.itemName = listBox.options[selIdx].text;
        newList[idx] = obj;
        idx = idx + 1;
    }

    while (idx < listBox.options.length) {
        obj = new Object();
        obj.itemValue = listBox.options[idx].value;
        obj.itemName = listBox.options[idx].text;

        newList[idx] = obj;
        idx = idx + 1;
    }

    $("#" + listBox.id).empty();

    for (var i = 0; i < newList.length; i++) {
        var opt = document.createElement("OPTION");
        opt.value = newList[i].itemValue;
        opt.text = newList[i].itemName;
        listBox.options.add(opt);
    }

    listBox.selectedIndex = isUp ? selIdx - 1 : selIdx + 1;
}

function OnMenuClick(menuId, menuType, menuUrl, popupWidth, popupHeight) {
    if (menuType == 0) {
        Utils.showProgressBar();
        document.location.href = SC_Common.basePath + menuUrl;
    }
    else if (menuType == 1) {
        Utils.openWinCenter("Menu_" + menuId, SC_Common.basePath + menuUrl, popupWidth, popupHeight);
    }
    else {
        try {
            var script = menuUrl.substring(1, menuUrl.length);
            eval(script);
        }
        catch (exception) {
        }
    }
}

function AddJsonToSelect(jsonData, listId) {
    $("#" + listId).empty();
    var list = $get(listId);

    var disabled = list.disabled;
    if (disabled) {
        list.disabled = false;
    }

    $.each(jsonData, function (index, optionData) {
        if (Utils.isNullOrEmpty(optionData) ||
            Utils.isNullOrEmpty(optionData.Value)) {
            return;
        }

        var optn = document.createElement("OPTION");
        optn.text = optionData.Text;
        optn.value = optionData.Value;
        optn.selected = optionData.Selected;
        list.options.add(optn);

    });

    if (list.options.length == 0) {
        var optn = document.createElement("OPTION");
        optn.text = "====선택====";
        optn.value = "0";
        optn.selected = true;
        list.options.add(optn);
    }
}


function InitTelerik() {
    if ($.telerik == null) {
        return;
    }
    $(".t-grid table > tbody > tr[class!='t-no-data']")
                .live("click", function () {
                    $(this).addClass('t-state-selected')
                .siblings()
                .removeClass('t-state-selected');
                })
            .live("mouseenter", $.telerik.hover)
            .live("mouseleave", $.telerik.leave);
}

String.prototype.count = function (s1) {
    return (this.length - this.replace(new RegExp(s1, "g"), '').length) / s1.length;
}

function CuteEditor_GetNewTableCode(code) {
    var newCode = code.toString();//.toLowerCase();

    if (newCode.indexOf("<TABLE") == 0) {
        var countTd = newCode.count('<TD>');
        var countTr = newCode.count('<TR>');

        var w = 500 / (countTd / countTr);
        var newTd = "<td style=\"border: solid 1px windowtext; font-family: 맑은 고딕; font-size: 10pt; height: 26px;" + "width: " + w + "px;\">";
        newCode = newCode.replace(/<TD>/g, newTd);
    }
    else if (newCode.indexOf("<div") == 0) {
        //alert(newCode);
        //newCode = newCode.replace(/<DIV>/g, "<div style=\"font-family: 맑은 고딕;\">");
        newCode = newCode.replace(/<div>/g, "<div style=\"font-family: 맑은 고딕; font-size: 10pt; \">");
    }

    //newCode = newCode.replace(/(<form[^\>]*\>)([\s\S]*)(\<\/form\>)/ig, "$2");
    
    return newCode;
}


function _CuteEditor_ShowPreview(editor) {
    var oldexec = editor.ExecCommand;
    editor.ExecCommand = function (cmd, ui, val) {
        //alert("ExecCommand: " + cmd);
        if (cmd == "Preview") {
            var content = editor.getHTML();

            var input = document.getElementById("_EditorContent");
            if (input == null) {
                input = document.createElement("input");
                input.setAttribute("type", "hidden");
                input.setAttribute("name", "_EditorContent");
                input.setAttribute("id", "_EditorContent");
                document.appendChild(input);
            }

            input.setAttribute("value", escape(content));
            Utils.openWinCenterScroll("Preview", SC_Common.basePath + "CuteSoft_Client/CustomPreview.html?contentid=_EditorContent", 800, 700);

            //var html = escape(content);                        
            //var href = SC_Common.basePath + "CuteSoft_Client/CustomPreview.html?contentid=_EditorContent";
            //var returnValue = window.showModalDialog(href, html, 'dialogHeight:700px;dialogWidth:800px;center:yes;dialogTitle:Preview;resize:yes;');
            return true;
        }
        else if (cmd == "Table") {
            //var table = $('#ce_thetable');
            //alert(table.length);
        }

        return oldexec.apply(this, arguments);
    }
}

//count down rowIndex
function onDataBound(e) {
    var grid = $('#Grid').data('tGrid')
    var total = grid.total;
    var start = total - (grid.currentPage - 1) * grid.pageSize;
    //var start = (grid.currentPage - 1) * grid.pageSize + 1;
    var rows = jQuery("#Grid table tbody tr");
    for (var i = 0; i < rows.length; i++) {
        var row = rows[i];
        if (row.cells.length > 3) {
            var firstCell = row.cells[0];
            firstCell.innerText = start - i;
            //firstCell.innerText = start + i;            
        }
    }

    // set value for Export....
    page = grid.currentPage;
    pageSize = grid.pageSize;
    orderBy = (grid.orderBy || '~');
    filter = (grid.filterBy || '~');
}

///check or uncheck all checkbox with params is "id of checkAll " and checkbox name attribute
function checkAll(chkId_All,chk_name)
{
   $(document).ready(function ()
   {
      $("#"+chkId_All).click(function ()
      {
         var checked_status = this.checked;
         $("input[@name="+chk_name+"]").each(function ()
         {
            this.checked = checked_status;
         });
      });
   }); 

}

function HideDownloadIframe(iframeName, divProgressId) {
    jQuery(String.format('#{0}', iframeName)).ready(function () {
        try {
            parent.setTimeout("Utils.hideProgressBarWithId('" + divProgressId + "');", 1500);
        } catch (e) {
        }
    });
}